﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using HRM.HRMServiceProxy;
namespace HRM
{
    public partial class frmMergeBookInfo : Form
    {
        bool isChange = false;
        DialogResult dlgResult;
        EmployeeClient wcfEmployee;
        EmployeeInfo EmployeeInfo;
        DoiSoClient wcfDoiSo;
        HRM_DoisoInfo HRM_DoisoInfo;
        MainServiceClient wcfMainService;
        RoleRightInfo listRole;
        DetailClient wcfDetail;
        int DoiSoID;      
        int EmployeeID;

        public delegate void AfterSave();
        public event AfterSave afterSave;


        public frmMergeBookInfo(int _DoiSoID, RoleRightInfo _listRole, int _EmployeeID)
        {
            InitializeComponent();
           // Language();
            dlgResult = DialogResult.Cancel;           
            txtDate.Text = DateTime.Now.ToString("dd/MM/yyyy");

            wcfDoiSo = Service.DoiSo();
            wcfEmployee = Service.Employee();
            wcfMainService = Service.MainService();
            wcfDetail = Service.Detail();

            if (_DoiSoID > 0)
            {
                HRM_DoisoInfo = wcfDoiSo.HRM_Doiso_GetInfo(_DoiSoID);
                Info2Form();
                GetEmployeeName(HRM_DoisoInfo.doiso_employeeID);
            }
            DoiSoID = _DoiSoID;

            if (_EmployeeID > 0)
            {
                GetEmployeeName(_EmployeeID);
            }

            listRole = _listRole;
            EmployeeID = _EmployeeID;
            lstEmployee.Visible = false;
            txtSIBookNo_Tmp.Focus();
            btnSave.Enabled = listRole.Create;            
        }

        private void Language()
        {
            if (this.Tag != null)
                this.Text = UpdateUI.GetString("denghihuongtrocap");
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            UpdateUI.Load_Panel(ref panel2);
        }

        private void Info2Form()//Hiển thị thông tin ra form
        {
            txtDate.Text = HRM_DoisoInfo.doiso_ngaynhap.ToString("dd/MM/yyyy");
            txtSIBookNo_Tmp.Text = HRM_DoisoInfo.doiso_sosotam.ToString();
            txtSIBookNo.Text = HRM_DoisoInfo.doiso_sosodoi.ToString();
            txtEmpName.Tag = HRM_DoisoInfo.doiso_employeeID;
            isChange = false;
        }

        private void Form2Info()//Chuyển dữ liệu từ form nhập vào Info
        {
            HRM_DoisoInfo = new HRM_DoisoInfo();
            HRM_DoisoInfo.doiso_ID = DoiSoID;
            HRM_DoisoInfo.doiso_branchID = Program.intBranchID;
            HRM_DoisoInfo.doiso_employeeID = Convert.ToInt32(txtEmpName.Tag);
            HRM_DoisoInfo.doiso_sosodoi = txtSIBookNo.Text;
            HRM_DoisoInfo.doiso_sosotam = txtSIBookNo_Tmp.Text;
            HRM_DoisoInfo.doiso_ngaynhap = DateTime.ParseExact(txtDate.Text, "dd/MM/yyyy", null);
            HRM_DoisoInfo.doiso_loai = "C";
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            txtDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
            txtSIBookNo.Text = "";
            txtSIBookNo_Tmp.Text= "";
            txtEmpName.Text = "";
            txtSIBookNo_Tmp.Focus();
        }

        bool CheckValid()
        {
            DateTime DateVal;
            if (!DateTime.TryParseExact(txtDate.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out DateVal))
            {
                txtDate.Focus();
                MessageBox.Show(UpdateUI.GetString("ngaykhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (txtSIBookNo_Tmp.Text.Trim().Length == 0)
            {
                MessageBox.Show("Phải nhập số sổ tạm", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtSIBookNo_Tmp.Focus();
                return false;

            }
            else if (txtSIBookNo_Tmp.Text.Trim().Length != 10)
            {
                MessageBox.Show("Số sổ tạm phải đủ 10 ký tự", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtSIBookNo_Tmp.Focus();
                return false;

            }
            if (txtSIBookNo.Text.Trim().Length == 0)
            {
                txtSIBookNo.Focus();
                MessageBox.Show("Phải nhập số sổ BHXH", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            else if (txtSIBookNo.Text.Trim().Length != 10)
            {
                txtSIBookNo.Focus();
                MessageBox.Show("Số sổ BHXH phải đủ 10 ký tự", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            else if (txtSIBookNo.Text.Trim().ToLower() == txtSIBookNo_Tmp.Text.Trim().ToLower())
            {
                txtSIBookNo.Focus();
                MessageBox.Show("Số sổ BHXH giống số sổ tạm", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            //if (wcfDetail.Detail_CheckExistSIBookNo(txtSIBookNo.Text, Program.strBranchCode))
            //{
            //    MessageBox.Show("Số sổ BHXH này đã được sử dụng, bạn hãy kiểm tra lại!", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    txtSIBookNo.Focus();
            //    txtSIBookNo.SelectAll();
            //    return false;
            //}
            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckValid())
            {
                isChange = true;
                return;
            }
            wcfDoiSo = Service.DoiSo();
            Form2Info();
           
            if (DoiSoID > 0)//update
            {
                if (!listRole.Update)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyencapnhat"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (wcfDoiSo.HRM_Doiso_Update(HRM_DoisoInfo))
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;                    
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
            else//insert
            {
                if (!listRole.Create)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                HRM_DoisoInfo.doiso_ID = wcfDoiSo.HRM_Doiso_Insert(HRM_DoisoInfo);
                DoiSoID = HRM_DoisoInfo.doiso_ID;
                if (DoiSoID > 0)
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("themmoithanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("themmoikhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
            DoiSoID = 0;
            txtDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
            txtSIBookNo.Text = "";
            txtSIBookNo_Tmp.Text = "";
            txtEmpName.Text = "";
            txtCardNo.Text = "";
            txtngaysinh.Text = "";
            txtSIBookNo_Tmp.Focus();
            if (afterSave != null)
                afterSave();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmEditInsBookInfo_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
            }
        }

        private void frmEditInsBookInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show(UpdateUI.GetString("bandathaydoidulieu") + "\n" + UpdateUI.GetString("coluutruockhithoatkhong"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
            wcfEmployee.Close();
            wcfMainService.Close();
            wcfDoiSo.Close();
            this.DialogResult = dlgResult;

        }
     
               

        private void frmEditBookInfo_Activated(object sender, EventArgs e)
        {
            Language();
        }

        private void DataChange(object sender, EventArgs e)
        {
            isChange = true;
        }

       
        private void btnSelectEmp_Click(object sender, EventArgs e)
        {
            frmEmployeeList f = new frmEmployeeList(true);
            if (f.ShowDialog() == DialogResult.OK)
            {
                GetEmployeeName(f.EmpID);
            }
        }
        void GetEmployeeName(int _EmployeeID)
        {
            EmployeeInfo = wcfEmployee.Employee_GetInfo(_EmployeeID);
            txtEmpName.Tag = EmployeeInfo.ID;
            txtEmpName.Text = EmployeeInfo.LastName + " " + EmployeeInfo.FirstName;
            txtCardNo.Text = EmployeeInfo.IDCardNo;
            txtngaysinh.Text = EmployeeInfo.DateOfBirth.ToString("dd/MM/yyyy");
        }

        private void frmEditBookInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show(UpdateUI.GetString("bandathaydoidulieu") + "\n" + UpdateUI.GetString("coluutruockhithoatkhong"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
            this.DialogResult = dlgResult;

        }

        private void txtSIBookNo_Tmp_TextChanged(object sender, EventArgs e)
        {
            if (txtSIBookNo_Tmp.Focused)
            {
                if (txtSIBookNo_Tmp.Text.Length > 0)
                {
                    AutoCompleteEmployee(txtSIBookNo_Tmp.Text);
                    if (lstEmployee.Items.Count > 0)
                    {
                        lstEmployee.Visible = true;
                        lstEmployee.HeaderStyle = ColumnHeaderStyle.None;
                        lstEmployee.BringToFront();
                        lstEmployee.Items[0].Selected = true;
                    }
                    else
                        lstEmployee.Visible = false;
                }
                else
                    lstEmployee.Visible = false;


            }
        }

        void AutoCompleteEmployee(string Parameter)
        {
            lstEmployee.Items.Clear();
            DataTable dtGL = LocalDB.Employee_GetListByParameter(Parameter.ToLower());
            ListViewItem item;
            if (dtGL != null)
                for (int i = 0; i < dtGL.Rows.Count; i++)
                {
                    item = new ListViewItem(dtGL.Rows[i]["Employee_SIBookNo"].ToString());
                    item.SubItems.Add(dtGL.Rows[i]["FullName"].ToString());
                    item.Name = dtGL.Rows[i]["FullName"].ToString();
                    item.SubItems.Add(Convert.ToDateTime(dtGL.Rows[i]["Employee_DateOfBirth"]).ToString("dd/MM/yyyy"));
                    item.Tag = dtGL.Rows[i]["Employee_ID"].ToString();
                    item.SubItems.Add(dtGL.Rows[i]["Employee_IDCardNo"].ToString());
                    lstEmployee.Items.Add(item);
                }
        }

        private void txtSIBookNo_Tmp_Enter(object sender, EventArgs e)
        {
            txtSIBookNo_Tmp.SelectAll();
        }

        private void txtSIBookNo_Tmp_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                if (lstEmployee.Items.Count > 0)
                {
                    lstEmployee.Focus();
                    for (int i = 0; i < lstEmployee.Items.Count; i++)
                    {
                        if (lstEmployee.Items[i].Selected)
                        {
                            txtSIBookNo_Tmp.Text = lstEmployee.Items[i].SubItems[0].Text;
                            txtEmpName.Text = lstEmployee.Items[i].SubItems[0].Name;
                            //_MedicalPlace = Employee_GetOne(txtSIBookNo.Text, txtEmpName.Text).Rows[0][0].ToString();

                        }
                    }

                }
            }
            else if (e.KeyCode == Keys.Right && (txtSIBookNo_Tmp.SelectionLength == txtSIBookNo_Tmp.Text.Length || txtSIBookNo_Tmp.SelectionStart == txtSIBookNo_Tmp.Text.Length))
            {
                this.ProcessTabKey(true);
                ((TextBox)sender).SelectAll();
                e.Handled = true;
            }
            else if (e.KeyCode == Keys.Left && (txtSIBookNo_Tmp.SelectionLength == txtSIBookNo_Tmp.Text.Length || txtSIBookNo_Tmp.SelectionStart == 0))
            {
                this.ProcessTabKey(false);
                ((TextBox)sender).SelectAll();
                e.Handled = true;
            }
        }

        private void lstEmployee_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //_listKeyUp = true;
                if (lstEmployee.Items.Count > 0)
                {
                    for (int i = 0; i < lstEmployee.Items.Count; i++)
                    {
                        if (lstEmployee.Items[i].Selected)
                        {
                            lstEmployee.Items[i].BackColor = Color.Yellow;
                            txtSIBookNo_Tmp.Text = lstEmployee.Items[i].SubItems[0].Text;
                            txtEmpName.Text = lstEmployee.Items[i].SubItems[0].Name;
                            txtngaysinh.Text = lstEmployee.Items[i].SubItems[2].Text;
                            txtCardNo.Text = lstEmployee.Items[i].SubItems[3].Text;
                            txtEmpName.Tag = lstEmployee.Items[i].Tag;
                        }
                    }
                }
                lstEmployee.SendToBack();
                txtSIBookNo_Tmp.Focus();
                lstEmployee.Visible = false;
            }
            //_listKeyUp = false;
        }

        private void lstEmployee_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            for (int i = 0; i < lstEmployee.Items.Count; i++)
            {
                if (lstEmployee.Items[i].Selected)
                {
                    txtSIBookNo_Tmp.Text = lstEmployee.Items[i].SubItems[0].Text;
                    txtEmpName.Text = lstEmployee.Items[i].SubItems[0].Name;
                    txtEmpName.Tag = lstEmployee.Items[i].Tag;
                }
            }
            lstEmployee.SendToBack();
            txtSIBookNo_Tmp.Focus();
            lstEmployee.Visible = false;
        }

        private void frmDoiSo_KeyUp(object sender, KeyEventArgs e)
        {               
            switch (e.KeyCode)
            {
                case Keys.F3:
                    if (btnSave.Enabled)
                        btnSave_Click(null, null);
                    break;                
                case Keys.Escape:
                    this.Close();
                    break;
                case Keys.Enter:
                ////if (!_listKeyUp)
              
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }                   
                    break;
            }
          }
             
    }
}
